#ifndef SERIALPORTUTILS_H
#define SERIALPORTUTILS_H
#include <QDebug>
#include <QObject>
#include <QSerialPort>
#include <serialportmonitorthread.h>

///状态枚
enum class ConnectionStatus {
    Disconnected,
    Connecting,
    Connected,
    ConnectionFailed
};
///串口工具类

class SerialPortUtils: public QObject
{
    Q_OBJECT
public:
    explicit SerialPortUtils(QObject *parent = nullptr);
    ~SerialPortUtils();

    //获取串口列表
    QStringList getAvailablePorts();
    // 连接到串口
    void connectToPort(const QString &portName = "", int baudRate = 9600 , const bool isAuto = true);

    // 断开串口
    void disconnectPort();

    // 发送数据到串口
    void sendData(const QByteArray &data);

    //判断是否连接
    bool isConnected() const;

signals:
    // 串口连接状态回调
    void connectionStatusChanged(const ConnectionStatus status, const QString &message = "");

    // 接收到串口数据回调
    void dataReceived(const QByteArray &data);

private slots:
    // 串口接收到数据的槽函数
    void onDataReceived();
private:
    //连接
    QPair<bool, QString>  _connectPort(const QString &portName, int baudRate);

    QSerialPort *serialPort;
    SerialPortMonitorThread *monitorThread = nullptr;
};



#endif // SERIALPORTUTILS_H
